/**
 */

package com.bei_en.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bei_en.teacher.admin.other.Consts;

public class HttpClientUtils {

	private final static Logger LOG = LoggerFactory
			.getLogger(HttpClientUtils.class);

	/**
	 *
	 * @author Guocg
	 * @param serverUrl
	 * @param data
	 * @param timeout
	 *            秒
	 * @return
	 */
	public static String post(String serverUrl, String data, int timeout) {

		HttpClient httpclient = HttpClientBuilder.create().build();
		HttpPost httppost = new HttpPost(serverUrl);
		try {
			httpclient.execute(httppost);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		StringBuilder responseBuilder = null;
		BufferedReader reader = null;
		OutputStreamWriter wr = null;

		URL url;
		try {
			url = new URL(serverUrl);
			URLConnection conn = url.openConnection();
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			// conn.setDoInput(true);

			conn.setConnectTimeout(1000 * timeout);
			wr = new OutputStreamWriter(conn.getOutputStream());

			wr.write(data);
			wr.flush();

			reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			responseBuilder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				responseBuilder.append(line).append("\n");
			}
			String re = responseBuilder.toString();
			LOG.debug(re);
			return re;
		} catch (SocketTimeoutException e) {
			LOG.error("超时", e);
			return "超时";
		} catch (IOException e) {
			LOG.error("", e);
		} finally {

			if (wr != null) {
				try {
					wr.close();
				} catch (IOException e) {
					LOG.error("close error", e);
				}
			}

			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					LOG.error("close error", e);
				}
			}

		}

		return "失败";
	}

	public static String get(String serverUrl, String data, int timeout) {

		StringBuilder responseBuilder = null;
		BufferedReader reader = null;
		OutputStreamWriter wr = null;

		URL url;
		try {
			url = new URL(serverUrl + "?" + data);
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(1000 * timeout);
			// 建立实际的连接
			conn.connect();

			reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			responseBuilder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				responseBuilder.append(line).append("\n");
			}
			String re = responseBuilder.toString();
			LOG.debug(re);
			return re;
		} catch (SocketTimeoutException e) {
			LOG.error("超时", e);
			return "超时";
		} catch (IOException e) {
			LOG.error("", e);
		} finally {

			if (wr != null) {
				try {
					wr.close();
				} catch (IOException e) {
					LOG.error("close error", e);
				}
			}

			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					LOG.error("close error", e);
				}
			}

		}

		return "失败";
	}

	public static void main(String args[]) {

		System.out.println(post(Consts.JIAOJING_URL + "/query/vioForce",
				"key=carNum&value=桂ATT376", 5));
	}

}
